package com.grape.service.base.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.grape.dao.base.CourierRepository;
import com.grape.dao.base.FixedAreaRepository;
import com.grape.dao.base.TakeTimeRepository;
import com.grape.domain.base.Courier;
import com.grape.domain.base.FixedArea;
import com.grape.domain.base.TakeTime;
import com.grape.service.base.FixedAreaService;

@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {

	@Autowired
	private CourierRepository cr;
	
	@Autowired
	private TakeTimeRepository ttr;
	
	@Autowired
	private FixedAreaRepository far;
	
	@Override
	public void save(FixedArea model) {
		far.save(model);
	}

	@Override
	public Page<FixedArea> findAll(Specification<FixedArea> sf, Pageable pageable) {
		return far.findAll(sf, pageable);
	}

	@Override
	public void associationCourierToFixedArea(FixedArea model, Integer courierId, Integer takeTimeId) {
		FixedArea fixedArea = far.findOne(model.getId());
		Courier courier = cr.findOne(courierId);
		TakeTime taketime = ttr.findOne(takeTimeId);
		// 没有保存操作,spring结合事物管理的强大之处
		// 事务结束时发现一级缓存与快照内容不匹配,更新数据库
		courier.setTakeTime(taketime);
		fixedArea.getCouriers().add(courier);
	}

	@Override
	public FixedArea findOne(String dqid) {
		return far.findOne(dqid);
	}

}
